<!-- 积分记录 -->
<template>
	<view class="page flex-box">
		<status-bar :title="nameDefinition?nameDefinition+'明细':'积分明细'"></status-bar>
		<backTop v-if='toTop'></backTop>
		<view class="headBox thmeBg" :style="{'--color': color}">
			<image :src="staticUrl+'/images/jh_up/interRecord-head.png'" mode="aspectFill" style="position: absolute;width: 100%;height: 100%;bottom: 0;left: 0;"></image>
			<view class="recentMonth flex-box align-center">
				<swiper class="swiper flex" display-multiple-items='3' :current="currentIndex" @change="change">
				    <swiper-item>
				       <view class="swiper-item"> </view>
				    </swiper-item> 
					 <swiper-item v-for="(item,index) in date" :key='index'>
				        <view class="swiper-item" :class="{
							active:currentIndex == index
							}" @click="tapMonth(index)">{{item.name}}</view>
				     </swiper-item>
					 <swiper-item>
					    <view class="swiper-item"> </view>
					 </swiper-item> 
				</swiper>
			</view>
			<view class="tatalIntegral">
				<view class="flex-box align-center flex-center">
					<image :src="staticUrl+'/images/jh_up/icon-coin1.png'" class="icon-coin" mode="heightFix"></image>
					<view class="weight">{{integral}}</view>
				</view>
				<text>总{{nameDefinition?nameDefinition:'积分'}}</text>
			</view>
			<view class="tatalIntegral1 flex-box align-center flex-between">
				<view>
					<view>{{earn}}</view>
					<view>本月总收入</view>
				</view>
				<view>
					<view>{{use}}</view>
					<view>本月总消耗</view>
				</view>
			</view>
		</view>
		<!-- 积分明细 -->
		<view class="recordBox flex">
			<view class="recordListTitle">{{nameDefinition?nameDefinition:'积分'}}收支明细</view>
			<view class="underline"></view>
			<scroll-view scroll-y="auto" class="recordList" @scrolltolower='scrolltolower' v-if='recordList.length > 0'>
				<block v-for="(item,index) in recordList" :key="index">
					<view class="recordItem flex-box flex-between align-center">
						<view class="recordLeft flex">
							<view class="recordName">{{item.title}}</view>
							<view class="recordDate">{{item.addDate}}</view>
						</view>
						<view>
							<text class="increase success-color" v-if='item.status == 1'>+{{item.jifen}}</text>
							<text class="decrease error-color" v-else>-{{item.jifen}}</text>
						</view>
					</view>
					<view class="underline"></view>
				</block>
				<view class="DiLine" v-if='recordList && recordList.length > 0 && !more'>----- 我也是有底线的 -----</view>
			</scroll-view>
			<view v-if='noData'>
				<empty type='1' cat = '4' :msg='"没有相关记录,签到就可以获得"+nameDefinition+"哦 ~"'></empty>
			</view>
		</view>
	</view>
</template>

<script>
	var that;
	export default {
		data() {
			return {
				
				color: getApp().globalData.color,
				toTop: false,
				
				currentIndex: 11,
				recordList: [],
				integral:0,
				earn:0,
				use:0,
				staticUrl: this.staticUrl,
				page:1,
				pageSize: 20,
				more: false,
				nameDefinition: uni.getStorageSync('startUp').nameDefinition.fenTitle?uni.getStorageSync('startUp').nameDefinition.fenTitle:'积分',
				date:[],
				noData: false
			}
		},
		onLoad(){
			that = this;
			let year = new Date().getFullYear();
			let month = new Date().getMonth()+1;
			for(var i = 0;i<12-month;i++){ // 去年
				that.date.push({
					year: year-1,
					month: i+month+1<10?'0'+(i+month+1):i+month+1,
					name: i+month+1+'月'
				})
			}
			for(var i = 0;i<month;i++){ // 今年
				that.date.push({
					year: year,
					month: i+1<10?'0'+(i+1):i+1,
					name: i+1+'月'
				})
			}
			this.getRecord(false);
			uni.setNavigationBarTitle({
				title: that.nameDefinition+'明细'
			})
		},
		onShareAppMessage(res) {
		    let shareData = uni.getStorageSync('shareData');
			let vid = uni.getStorageSync('vid');
		    return {
				title: shareData.share_title,
				desc: shareData.share_desc,
				path: '/pages/index/index?share_v_id='+vid,
				imageUrl: shareData.share_img
		    }
		},
		onPageScroll(e) {
			if (e.scrollTop > 300) {
				that.toTop = true
			} else {
				that.toTop = false
			}
		},
		onReachBottom() {
			if(that.more){
				that.getRecord(true);
			}
		},
		methods: {
			scrolltolower(){
				let that = this
				if(that.more){
					that.getRecord(true);
				}
			},
			getRecord(flag){
				let that = this;
				that.more = false;
				if(!flag){
					that.recordList = []
					that.page = 1
				}
				that.getUserIntegral()
				that.$http.post({
					url: '/SRA_userIntegral/jifenRecord',
					data: {
						VeriCode:uni.getStorageSync('bid'),
						vId:uni.getStorageSync('vid'),
						Y: that.date[that.currentIndex].year,
						M: that.date[that.currentIndex].month,
						pageIndex: that.page,
						pageSize: that.pageSize,
					}
				}).then(res => {
					if(res.code == 100){
						if(res.data){
							that.noData = false
							that.recordList = that.recordList.concat(res.data);
						}
						if(res.data && res.data.length > that.pageSize-1){
							that.more = true
							that.page ++
						}
					}else{
						uni.showToast({
							icon:"none",
							title: res.msg,
							duration:2000
						})
					}
					if(that.recordList.length < 1){
						that.noData = true
					}
					uni.hideLoading()
				}) 
			},
			getUserIntegral(){
				let that = this
				that.$http.post({
					url: '/SRA_userIntegral/thisMonthJifen',
					data: {
						VeriCode:uni.getStorageSync('bid'),
						vId:uni.getStorageSync('vid'),
						Y: that.date[that.currentIndex].year,
						M: that.date[that.currentIndex].month,
					}
				}).then(res => {
					if(res.code == 100){
						that.integral = res.data.jifen
						that.earn = res.data.gainJifen;
						that.use = res.data.minusJifen;
					}else{
						uni.showToast({
							icon:"none",
							title: res.msg,
							duration:2000
						})
					}
				}) 
			},
			change(e){
				this.tapMonth(e.detail.current)
			},
			tapMonth(index){
				if(index == this.currentIndex){
					return
				}
				if(index < this.currentIndex+1){
					this.prevMonth()
				}
				if(index > this.currentIndex){
					this.nextMonth()
				}
			},
			prevMonth(){
				if(this.currentIndex > 0){
					this.currentIndex --
					uni.showLoading({
						title:'加载中'
					})
					this.getRecord(false);
				}
			},
			nextMonth(){
				if(this.currentIndex < 11){
					this.currentIndex ++
					uni.showLoading({
						title:'加载中'
					})
					this.getRecord(false);
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.page{
		background-color: #F5F5F5;
		flex-direction: column;
		height: 100vh;
	}
	.underline{height: 1px;background-color: #EEEEEE;}
	.icon-xiangyouxiayiye,.icon-xiangzuo{
		color: white;
		font-size: 28rpx;
	}
	.headBox{
		padding: 24rpx 36rpx 84rpx;
		position: relative;
	}
	.recentMonth{margin: 0 66rpx 0 66rpx;height: 52rpx;padding: 0 26rpx;position: relative;z-index: 33;}
	.swiper{height: 100%;margin: 0 14rpx;}
	.swiper swiper-item{text-align: center;line-height: 52rpx;}
	.swiper-item{color: white;font-size: 24rpx;opacity: 0.5;}
	.icon-jiantouyou{color: white;font-size: 28rpx;}
	.icon-jiantouyou:nth-last-of-type(2){transform: rotate(180deg);}
	/* 当前查看的月份 */
	.swiper-item.active{font-size: 32rpx;opacity: 1;} 
	/* 未来的月份 */
	.swiper-item.future{opacity: .6;}
	.conotClick{opacity: .4;}
	.tatalIntegral{
		text-align: center;
		.icon-coin{
			width: 72rpx;
			height: 72rpx;
			margin-right: 20rpx;
		}
		.weight{
			font-size: 80rpx;
			color: white;
			font-weight: bold;
			letter-spacing: -4rpx;
		}
		text{
			font-size: 28rpx;
			color: white;
		}
		margin: 60rpx 0 30rpx;
	}
	.tatalIntegral1{
		padding: 0 68rpx;
		view{
			text-align: center;
			font-size: 22rpx;
			color: white;
		}
	}
	.midLine{display: inline-block;margin: 0 30rpx;width: 2rpx;height: 34rpx;background-color: white;transform: scale(0.5);}
	/* 日历 */
	.recordBox{
		background-color: #FFFFFF;
		margin: 0 30rpx;
		flex-direction: column;
		.recordListTitle{
			margin-top: -62rpx;
			color: #333333;
			font-size: 28rpx;
			line-height: 84rpx;
			padding: 0 24rpx;
			position: relative;
			z-index: 222;
			font-weight: bold;
			background-color: #FFFFFF;
			border-radius: 16rpx 16rpx 0 0;
		}
		.recordList{
			height: calc(100vh - 480rpx);
			.recordItem{
				padding: 20rpx 24rpx;
			}
			.recordLeft{
				margin-right: 20rpx;
			}
			.recordDate{color: #999999;font-size: 22rpx;}
			.recordName{color: #333333;margin-bottom: 10rpx;}
		}
	}
	
	
	
</style>
